/**
 * LS_UpdatePacket.java
 *
 * @author Hagen Boehm
 */


package SSF.OS.OSPFv2;


import java.util.*;
import SSF.OS.*;


// ========================= class SSF.OS.OSPFv2.LS_UpdatePacket ============================ //
/**
 * This class implements the payload of an OSPF Link State Update Packet.
 */
public class LS_UpdatePacket extends ProtocolMessage {

  private int numOfLSAs;
  private Vector lsas;
  // sum of the sizes of the LSAs stored in this structure.
  private int sizeOfLSAs;

  // ----------------------------- constructor LS_UpdatePacket ---------------------------- //

  public LS_UpdatePacket() {
    numOfLSAs = 0;
    lsas      = new Vector();
    sizeOfLSAs = 0;
  }

  // --------------------------------- LS_UpdatePacket.put --------------------------------- //
  /** 
   *  append an LSA to this LS Update Packet payload.
   */
  public void put(LSA lsa) {
    lsas.add(lsa);
    numOfLSAs++;
    sizeOfLSAs += lsa.bytecount();
  }

  // ------------------------------ LS_UpdatePacket.getLSAs ------------------------------- //
  /**
   *  return a Vector of all LSAs contained in this LS Update Packet payload.
   */
  public Vector getLSAs() {
    return lsas;
  }

  // ----------------------------- LS_UpdatePacket.numberOfLSAs ---------------------------- //
  /**
   *  return the total number of LSAs stored in this LS Update payload.
   */
  public int numberOfLSAs() {
    return numOfLSAs;
  }

  // ----------------------------- LS_UpdatePacket.carryPayload ---------------------------- //

  public void carryPayload(ProtocolMessage payload) {
    System.err.println("** Warning: LS_UpdatePacket can't take payload; dropping!");
  }

  // ------------------------------- LS_UpdatePacket.bytecount ---------------------------- //
  /** Return the total size (in bytes) of this LS Update Packet body. 
   *  All and only fields specified in RFC 2328 are included in calculation!!!
   */
  public int bytecount(){
    return header_bytecount();
  }

  // ---------------------------- LS_UpdatePacket.header_bytecount ------------------------- //
  /** Return the size (in bytes) of this header only, don't include a payload. 
   *  Link State Update Packets don't have payload.
   */
  public int header_bytecount(){
    return (4 + sizeOfLSAs);
  }

  // ---------------------------- LS_UpdatePacket.toString -------------------------- //
  public String toString() {
    String s = "";
    s = (new Integer(numOfLSAs)).toString();
    for (Enumeration e = lsas.elements() ; e.hasMoreElements() ;) {
              s += " " + e.nextElement().toString();

          }  
    return s;
  }

} //end class LS_Update_Packet


